package com.lxl.bt.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxl.bt.R;
import com.lxl.bt.activity.AgentIncomeActivity;
import com.lxl.bt.activity.BaseFragment2;
import com.lxl.bt.bean.AgentBean;
import com.lxl.bt.bean.AgentListBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @创建时间 2021/4/14 下午5:17
 * @类描述 一句话描述 你的UI
 */
public class MyAgentFragment extends BaseFragment2 {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int pageNo = 0;
    private int pageSize = 20;
    private List<String> sort = new ArrayList<>();
    private AgentAdapter agentAdapter;
    private boolean loadData = false;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_agent;
    }

    public static Fragment newInstance(int type) {
        MyAgentFragment myAgentFragment = new MyAgentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        myAgentFragment.setArguments(bundle);
        return myAgentFragment;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        int type = bundle.getInt("type");
        sort.clear();
        sort.add("id,desc");

        //下拉刷新
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            pageNo = 0;
            getData(type);
        });
        refreshLayout.setEnableLoadMore(true);
        //上拉加载
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                getData(type);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(agentAdapter = new AgentAdapter(null));

        agentAdapter.setOnItemClickListener((baseQuickAdapter, view1, i) -> {
            AgentBean agentBean = (AgentBean) baseQuickAdapter.getData().get(i);
            AgentIncomeActivity.intent(mContext,Integer.parseInt(agentBean.userId));
        });

        //加载数据
        refreshLayout.autoRefresh();
    }

    private void setEmptyView() {
        View emptyView = View.inflate(mContext, R.layout.empty_view, null);
        agentAdapter.setEmptyView(emptyView);
    }

    private void getData(int type){
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .agentList(pageNo,pageSize,type,sort).enqueue(new NetCallback<Result<AgentListBean>>() {
            @Override
            public void onSuccess(Result<AgentListBean> result) {
                if (result.isSuccess()) {
                    if (!isAdded()){
                        return;
                    }
                    AgentListBean data = result.getData();
                    if (pageNo == 0){
                        //刷新
                        agentAdapter.setNewData(data.content);
                        refreshLayout.finishRefresh();
                        refreshLayout.setEnableLoadMore(true);
                        if (data.content == null || data.content.size() == 0){
                            setEmptyView();
                        }
                    }else{
                        //加载更多
                        refreshLayout.finishLoadMore();
                        if (data.content != null){
                            agentAdapter.addData(data.content);
                        }
                    }
                    if (data.content == null || data.content.size() < pageSize) {
                        //没有更多了
                        refreshLayout.setEnableLoadMore(false);
                    }else{
                        refreshLayout.finishLoadMore();
                    }
                    pageNo++;
                }else{
                    ToastUtil.showToast(mContext,result.getMsg());
                }
            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", errorStr);
            }
        });
    }

    class AgentAdapter extends BaseQuickAdapter<AgentBean, BaseViewHolder>{

        public AgentAdapter(@Nullable List<AgentBean> data) {
            super(R.layout.item_agent, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, AgentBean agentBean) {
            baseViewHolder.setText(R.id.tv_user_name,agentBean.userName);
            baseViewHolder.setText(R.id.tv_date,agentBean.createDate);
            baseViewHolder.setText(R.id.tv_vis_code,getContext().getString(R.string.wk_visit_code,agentBean.createDate));
            baseViewHolder.setText(R.id.tv_week_income,agentBean.lastSevenIncomes);
            baseViewHolder.setText(R.id.tv_total_income,agentBean.totalIncomes);
        }
    }
}











